<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="完工单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['worksheetOdd']" placeholder="请输入完工单号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['customerNumber']" placeholder="请输入客户"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="货品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['itemCode']" placeholder="请输入货品编码"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item readOnly label="货品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['itemName']" placeholder="请输入货品名称"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['specifications']" placeholder="请输入规格"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['unit']" placeholder="请输入单位"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['number']" placeholder="请输入数量"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="审核" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-select ref="select" v-model="dataList.auditState" style="width: 100%" @change="sift">
                            <a-select-option v-for="(elem,index) in options" :key="index" :value="elem.label">{{elem.label}}</a-select-option>
                        </a-select>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-tag type="list" v-decorator="['warehouseId', validatorRules.warehouseId]" :trigger-change="true" dictCode="bas_warehouse,name,id" @sendName="getName" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="库位号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input required v-decorator="['location', validatorRules.location]" placeholder="请输入库位号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="入库类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['warehousingType']" placeholder="请输入入库类型"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction
} from '@/api/manage'
import pick from 'lodash.pick'

export default {
    name: "WarehousingAuditModal",
    components: {},
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            model: {},
            dataList: {
                auditState: ''
            },
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },
            options: [{
                value: '通过',
                label: '通过'

            }, {
                value: '不通过',
                label: '不通过'

            }],
            confirmLoading: false,
            validatorRules: {
                warehouseId: {
                    rules: [{
                        required: true,
                        message: '请选择仓库!'
                    }]
                },
                location: {
                    rules: [{
                        required: true,
                        message: '请选择库位号!'
                    }]
                },
            },
            url: {
                add: "/production/warehousingAudit/add",
                edit: "/production/warehousingAudit/audit",
            }
        }
    },
    created() {},
    methods: {
        sift(value) {
            this.$set(this.dataList, 'auditState', value);
        },
        getName(value) {
            this.$set(this.dataList, 'warehouse', value)
        },
        add() {
            this.edit({});
        },
        edit(record) {
            if (record.auditState == '待审核' || record.auditState == '未审核' || record.auditState == '不通过') {
                this.form.resetFields();
                this.dataList = {
                    auditState: record.auditState,
                    warehouse: record.warehouse
                }
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'worksheetOdd', 'customerNumber', 'itemCode', 'itemName', 'specifications', 'unit', 'number', 'warehouseId', 'location', 'warehousingType'))
                })
            } else {
                this.$message.warning('已审核,状态不可逆!')
            }
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    Object.assign(values, this.dataList);
                    let formData = Object.assign(this.model, values);
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                    })
                }

            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'worksheetOdd', 'customerNumber', 'itemCode', 'itemName', 'specifications', 'unit', 'number', 'warehouseId', 'location', 'warehousingType'))
        },

    }
}
</script>
